package zookeeper.client;

import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;

@Slf4j
public class BaseOperations extends StandaloneBase {

    private String first_node = "/first-node";

    /**
     * 同步  创建  临时节点
     */
    @Test
    public void testCreate() throws KeeperException, InterruptedException {
        ZooKeeper zooKeeper = getZooKeeper();

        String s = zooKeeper.create(first_node, "first".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        log.info("Create:{}", s);
    }

    /**
     * 异步  创建  持久节点
     */
    @Test
    public void testCreateAsync() {
        ZooKeeper zooKeeper = getZooKeeper();
        zooKeeper.create(first_node, "second".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, (rc, path, ctx, name) -> log.info("rc:{}, path:{}, ctx:{}, name:{},", rc, path, ctx, name), "context");
    }

    /**
     * 获取节点数据，并设置一个监听
     */
    @Test
    public void testGetData() {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if (event.getPath() != null
                        && event.getPath().equals(first_node)
                        && event.getType() == Event.EventType.NodeDataChanged) {
                    log.info("PATH: {}  发现变化", first_node);
                    try {
                        byte[] data = getZooKeeper().getData(first_node, this, null);
                        log.info(" data: {}", new String(data));
                    } catch (KeeperException | InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        try {
            byte[] data = getZooKeeper().getData(first_node, watcher, null);
            log.info(" data: {}", new String(data));
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改节点数据
     */
    @Test
    public void testSetData() throws KeeperException, InterruptedException {
        ZooKeeper zooKeeper = getZooKeeper();

        Stat stat = new Stat();
        // 获取节点数据和节点状态
        byte[] data = zooKeeper.getData(first_node, false, stat);
        log.info(first_node + ":{}", new String(data));
        int version = stat.getVersion();
        log.info("version:{}", version);

        // 修改节点数据
        Stat stat1 = zooKeeper.setData(first_node, "fourth".getBytes(), version);
        System.out.println(stat1);
    }

    /**
     * 删除节点
     */
    @Test
    public void testDelete() throws KeeperException, InterruptedException {
        // -1 代表匹配所有版本，直接删除
        // 任意大于 -1 的代表可以指定数据版本删除
        getZooKeeper().delete(first_node, -1);
        log.info("删除成功");
    }

    @Test
    public void asyncTest() {
        getZooKeeper().getData("/test", false, (rc, path, ctx, data, stat) -> {
            Thread thread = Thread.currentThread();
            log.info("Thread Name: {}, rc:{}, path:{}, ctx:{}, data:{}, stat:{}", thread.getName(), rc, path, ctx, data, stat);
        }, "test");
        log.info("over .");
    }
}
